package gov.pbc.nn.kjgl.AbroadApproval.ureport;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

public class A2ResizeImage {
	
	
	
	 public static OutputStream resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
	        BufferedImage prevImage = ImageIO.read(is);
	        double width = prevImage.getWidth();
	        double height = prevImage.getHeight();
	        double percent = size/width;
	        int newWidth = (int)(width * percent);
	        int newHeight = (int)(height * percent);
	        BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
	        Graphics graphics = image.createGraphics();
	        graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
	        ImageIO.write(image, format, os);
	        os.flush();
	        is.close();
	        os.close();
	        //ByteArrayOutputStream b = (ByteArrayOutputStream) os;
	        return os;
	    }
	
}
